package com.myiam.idm.portal.helper;

import java.util.Iterator;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.myiam.idm.core.model.Role;

public class RoleModelToViewHelper {

	
	public static TreeNode roleModelToTree (Role roleModel) {
		
    	TreeNode root = new DefaultTreeNode("root", null);		
		
    	if (roleModel.hasChildren()) {
    		addChildrenToTree (root, roleModel);
    	}
    	return root;
    	
    }

	private static void addChildrenToTree(TreeNode parent, Role roleModel) {
		Iterator<Role> it = roleModel.getChildrenAsIterator();
		
		while (it.hasNext()) {
			
			Role child = it.next();
			TreeNode childNode = new DefaultTreeNode(child, parent);
			if (child.hasChildren()) {
				addChildrenToTree(childNode, child);
			}
		}
		
	
	}
}